---
title: "Class 2"
author: "Colin Kuehl"
date: "`r format(Sys.time(), '%B %d, %Y')`"
format: pdf
editor_options: 
  chunk_output_type: console
---

Today we'll explore the gapminder data. It contains life expectancy, population, and GDP per capita for countries every five years from 1952 to 2007. The gapminder data comes from the Gapminder Foundation: <https://www.gapminder.org/data/> made famous by Hans Rosling's TED talks. We'll continue our exploration using R to understand and visualize data.

# Part 1: Getting to Know Gapminder

Make sure you have opened the Rproject first.

Today's data comes packaged inside R itself. Instead of loading a file, we install a package once, then load it with library() each session.

Packages are like bonus material for R. We will use them all the time. Installing puts it on your computer. Library opens it(like a book) so everytime you use it you need library in your code first.

```{r}
#install.packages("gapminder") #only run this once ever - then put the # back in front
library(gapminder)
data("gapminder") #Puts into our environment

#install.packages("dplyr") #We'll use this package later, but always a good idea to load packages and data the beginning of your R script
library(dplyr)

```

Looking over the data using commmands from yesterday

(hint: use code from yesterday)

```{r}
head(gapminder)
tail(gapminder)
summary(gapminder)
dim(gapminder) #Count of how many observations and variables

str(gapminder) #This tells us what kind of data R thinks each variable is - we'll talk about this later
length(unique(gapminder$country)) #gives a count of unique countrys
length(unique(gapminder$year))
```

Checking in (with a partner)

1)  How many countries are in the dataset?
2)  What is the average life expectancy?
3)  What is the median GDP per capita? Average? 
<!-- -->
a)  Create a scatterplot with the relationship between population and Life Expectancy. Add a trend line b)What country has the highest life expectancy?

```{r}
length(unique(gapminder$country))

mean(gapminder$lifeExp)
median(gapminder$gdpPercap)

```

*It is best practice to "render as you go." Try rendering now to make sure you don't have errors in your code.*

Country and continent are not continuous(numeric in R speak) variables. They are categorical. We can get counts in each category.

frequency tables:

```{r}
table(gapminder$continent) #gives count of different categories
table(gapminder$year)

prop.table(table(gapminder$continent)) #gives percentages
```

I use this command all the time to get a sense of the data.

Check in: What percentage of the observations in the dataset are from 2007?

```{r}
prop.table(table(gapminder$year))
```

\`\`

Plot the relationship between GDP per capita and life expectancy

```{r}
plot(gapminder$gdpPercap, gapminder$lifeExp) #scatterplot (X then Y)
abline(lm(gapminder$lifeExp~gapminder$gdpPercap)) #add trend line (Y then X) 
```

Notice how we always put the explanatory variable on the X axis(horizantal) and the outcome variable on the Y axis (vertical)

GDP per capita hard to read - change unit to thousands

```{r}
gapminder$gdpPercap <- gapminder$gdpPercap/1000
```

Make plot look a bit more professional

```{r}
plot(gapminder$gdpPercap, gapminder$lifeExp, main="Relationship between Wealth and Life Expectancy", xlab="GDP per Capita", ylab="Life Expectancy")
abline(lm(gapminder$lifeExp~gapminder$gdpPercap)) #and boom, you've already ran your 2nd regression
```

Does there appear to be a relationship in the sample?

What about with just Asia? There are multiple ways to do this:

We can create a new dataset ie a subset(I almost always choose this option)

```{r}
table(gapminder$continent)
asia <- gapminder[which(gapminder$continent=="Asia"),]
asia <- filter(gapminder, continent=="Asia") # This does the same thing using the dplyr package from tidyverse. I find it much more intuitive than "base R" so tend to use it more often. 



plot(asia$gdpPercap, asia$lifeExp, main="Relationship between Wealth and Life Expectancy (Asia)", xlab="GDP per Capita", ylab="Life Expectancy")
abline(lm(asia$lifeExp~asia$gdpPercap)) #and boom, another regression

```

Exclude Europe

```{r}
noteuro <- gapminder[which(gapminder$continent!="Europe"),] # the != does not equal
noteuro <- filter(gapminder, continent!="Europe")
```

Exclude the super rich - use greater than or less than

```{r}
notrich <- gapminder[which(gapminder$gdpPercap< 50),] # less than 50,000 - this drops oil-rich outliers like Kuwait
notrich <- filter(gapminder, gdpPercap< 50)
```

Check in: Try creating a scatterplot with just the 1) Africa OR 2) country-years where life expectancy is less than 70.

```{r}
africa <- filter(gapminder, continent=="Africa")

plot(africa$gdpPercap, africa$lifeExp, main="Relationship between Wealth \n and Life Expectancy (Africa)", xlab="GDP per Capita", ylab="Life Expectancy", col=gapminder$country)
abline(lm(africa$lifeExp~africa$gdpPercap)) #and boom, another regression


early <- filter(gapminder, lifeExp < 70)

plot(early$gdpPercap, early$lifeExp, main="Relationship between Wealth \n and Life Expectancy under 70", xlab="GDP per Capita", ylab="Life Expectancy", col=early$continent)
abline(lm(early$lifeExp~early$gdpPercap), col="lightgreen") #and boom, another regression

table(early$country)
```

#Looking at the data over time: Time to look at something a bit more fun. How has life expectancy changed across the world since the 1950s?

Mean life expectancy?

```{r}
mean(gapminder$lifeExp)

min(gapminder$lifeExp)

max(gapminder$lifeExp) 

range(gapminder$lifeExp) 
```

Create a histogram for life expectancy

```{r}
hist(gapminder$lifeExp)
abline(v=mean(gapminder$lifeExp), col="blue", lwd=2)#adds a vertical(v) line for the mean
abline(v=median(gapminder$lifeExp), col="red", lwd=2)#a a line for median
```

abline gives us a line. v is for vertical

Density plots and boxplots show us similar information

```{r}
boxplot(gapminder$lifeExp)

plot(density(gapminder$lifeExp))

#How can we find out which country-years are at the very bottom?
```

Check: How many observations are from Africa and how many are from Europe

```{r}


```

```{r}
mean(gapminder$lifeExp[gapminder$continent=="Africa"]) #average for Africa
mean(gapminder$lifeExp[gapminder$continent=="Europe"]) #average for Europe
mean(asia$lifeExp)

#find the mean for the Americas

```

Let's make a simple scatterplot with year on the horizontal axis, and life expectancy on the vertical axis. A regression allows us to look at the relationship between variables linear regression model, and you're looking at the effect of the IV on the DV

```{r}
plot(gapminder$year, gapminder$lifeExp, ylab = "Life Expectancy", xlab = "Year", main = "Life Expectancy Over Time")
abline(lm(gapminder$lifeExp ~ gapminder$year), col="red", lwd=2) # What type of relationship is this?
abline(h=mean(gapminder$lifeExp), col="blue", lwd=2)
```

That is a bit overwhelming lets just look at 2007 - ie lets create a subset

```{r}
g2007 <- gapminder[which(gapminder$year==2007),] #just using base R

#New Graph
plot(g2007$gdpPercap, g2007$lifeExp, ylab = "Life Expectancy", xlab = "GDP per Capita", main = "Wealth and Life Expectancy in 2007", col=g2007$continent) #color for continent
abline(lm(g2007$lifeExp ~ g2007$gdpPercap), col="red", lwd=2)
legend("bottomright", legend = levels(factor(g2007$continent)), pch=19, col = factor(levels(factor(g2007$continent))))

#I want to add labels for specific countries
g2007$stars <- ifelse(g2007$country=="United States"|g2007$country=="China" |g2007$country=="Myanmar" |g2007$country=="Thailand", as.character(g2007$country), "") #create a new variable for notable countries, all else will be empty. 
text(g2007$gdpPercap, g2007$lifeExp-1, g2007$stars, cex=.4) #add text to location
```

Check: Can you create the same for 1952

```{r}

```

A boxplot for each year can show us how the distribution has changed over time

```{r}
boxplot(gapminder$lifeExp~gapminder$year, xlab = "Year", ylab="Life Expectancy", border = "steelblue")

boxplot(gapminder$lifeExp~gapminder$continent, xlab = "Year", ylab="Life Expectancy", border = "steelblue")
```
quarto install tinytex

Lets look at changes in thailand over time

```{r}
thai <- filter(gapminder, country=="Thailand")
plot(thai$year, thai$lifeExp, xlab="Year", ylab="Life Expectancy", col="red", main="Thailand Life Expectancy")
lines(thai$year, thai$lifeExp, col="blue")
```

```{r}
plot(asia$year, asia$lifeExp, xlab="Year", ylab="Life Expectancy", col=gapminder$country, main="Thailand Life Expectancy")
lines(thai$year, thai$lifeExp, col="blue")
```

Check: Do the same above for a different country

I've clearly had too much fun. Your turn to make something interesting.

```{r}



```

Since we've created some new variables we may want to save our data:

```{r}
write.csv(g2007, "g2007.csv") #writes a csv to your working directory that you can open in excel
save(g2007, file="g2007.Rdata") #saves Rdata format to your working directory
```
